﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Maticsoft.Common
{
    public class Util2
    {
        public static bool CheckDataSet(DataSet ds)
        {
            if (ds == null || ds.Tables.Count <=0 || ds.Tables[0].Rows.Count <= 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>  
        /// 时间戳转为C#格式时间  
        /// </summary>  
        /// <param name="timeStamp">Unix时间戳格式</param>  
        /// <returns>C#格式时间</returns>  
        public static DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }
        /// <summary>  
        /// DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time"> DateTime时间格式</param>  
        /// <returns>Unix时间戳格式</returns>  
        public static int ConvertDateTimeInt(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

        public static string GetModelData<T>(string key,T data)
        {
            if (data != null)
            {
                try
                {
                    Type type = data.GetType();
                    PropertyInfo pi = type.GetProperties().FirstOrDefault(x => x.Name == key);
                    return pi.GetValue(data, null).ToString();
                }
                catch
                {

                }

            }
            return "";
        }

        public static string JustifyPv(int pv)
        {
            if (pv < 4980)
            {
                pv = 4980;
            }
            if (pv > 100000)
            {
                return "100000+";
            }
            else
            {
                return pv.ToString();
            }

        }

        public static string JustifyClickLike(int clicklike)
        {
            if (clicklike < 135)
            {
                clicklike = 135;
            }
            if (clicklike > 10000)
            {
                return "10000+";
            }
            else
            {
                return clicklike.ToString();
            }
        }

        public static string ProcRegText(string str, string relacestr, string regstr)
        {
            RegexOptions ops =  RegexOptions.IgnoreCase;
            Regex reg = new Regex(regstr, ops);
            MatchCollection matches = reg.Matches(str);

            foreach (Match match in matches)
            {
                GroupCollection groups = match.Groups;
                //提取匹配项内的分组信息
                for (int i = 0; i < groups.Count; i++)
                {
                }
            }
            return "";

        }

        /// <summary>
        ///     Gets a value indicating whether the client is a mobile device.
        /// </summary>
        /// <value><c>true</c> if this instance is mobile; otherwise, <c>false</c>.</value>
        public static bool IsMobile
        {
            get
            {
                var context = HttpContext.Current;
                if (context != null)
                {
                    var request = context.Request;
                    if (request.Browser.IsMobileDevice)
                    {
                        return true;
                    }
                    AppCommon.Instance.LoggerHandler.Log(request.UserAgent);
                    if (!string.IsNullOrEmpty(request.UserAgent) && RegString.RegexMobile.IsMatch(request.UserAgent.ToLower()))
                    {
                        return true;
                    }
                }

                return false;
            }
        }

        //获取真实IP
        public static string GetIp()
        {
            //可以隔过代理IP获得真实IP
            string userIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            //没有代理服务器,如果有代理服务器获取的是代理服务器的IP
            if (userIP == null || userIP == "")
            {
                userIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            return userIP;
        }

        /// <summary>
        /// 获取HTTP请求的Referer
        /// </summary>
        /// <param name="ishost">Referer为空时是否返回Host（网站首页地址）</param>
        /// <returns>string</returns>
        public static string GetReferer(bool ishost)
        {
            if (HttpContext.Current.Request.UrlReferrer != null)
            {
                return HttpContext.Current.Request.UrlReferrer.ToString();
            }
            else
            {
                if (ishost)
                {
                    return HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority;
                }
                else
                {
                    return "";
                }
            }
        }
    }
}
